<template>
    <div class="product">
      <!-- 按钮开始 -->
       <div class="button">
        
        <el-button class="bu" @click="showHandler" size='mini' type="primary">添加</el-button>
        <el-input class="sm"  size="mini" placeholder="请输入名称"></el-input>
        <div>
            <el-select v-model="select" style="margin: 0 15px;" slot="prepend" size="mini" placeholder="请选择状态">
               <el-option label="正常" value="1"></el-option>
               <el-option label="下架" value="2"></el-option>
            </el-select>
            <el-select v-model="select1" slot="prepend" size="mini" placeholder="请选择栏目">
               <el-option label="家庭保洁" value="1"></el-option>
               <el-option label="衣物护理" value="2"></el-option>
               <el-option label="母婴陪护" value="3"></el-option>
               <el-option label="家具养护" value="4"></el-option>
               <el-option label="家电清洗" value="5"></el-option>
               <el-option label="环境整治" value="6"></el-option>
            </el-select>
        </div>
        <el-button class="bu" style="margin-left: 15px;" type="primary" size='mini'>查询</el-button>
       </div>
        <!-- 按钮结束 -->
        <!-- 表格开始 -->
        <el-table
        :data="products"
        style="width: 100%">
        <el-table-column
            prop="id"
            label="产品编号"
            width="90"
            align='center'>
        </el-table-column>
        <el-table-column
            prop="name"
            label="名称" align='center'>
        </el-table-column>
        <el-table-column
        label="图片"
        width="180px"
        align="center">
        <template slot-scope="scope">
            <el-popover trigger="hover"  placement="top">
                <img width="100%" :src="scope.row.photo">
                <div slot="reference" class="name-wrapper">
                    <img width="30px" :src="scope.row.photo">
                </div>
            </el-popover>
        </template>
        </el-table-column>
        <el-table-column prop="price" label="价格" align='center'>
        </el-table-column>
        <el-table-column prop="status" label="状态" align='center'>
        </el-table-column>
        <el-table-column prop="category.name" label="所属栏目" align='center'>
        </el-table-column>
        <el-table-column width='160px' label="操作" align='center'>
           <template slot-scope="scope">
                <el-button @click='editHandler(scope.row)' style="background-color: #fff;color:#49b99d;border:none" size="mini" type="primary">编辑</el-button>
                <el-button @click="deleteHandler(scope.row.id)" style="background-color: #fff;color:red;border:none" size="mini" type="danger">下架</el-button>
            </template>
        </el-table-column>
        </el-table>
        <!-- 表格结束 -->
        <!-- 分页开始 -->
        <div class="block">
            <el-pagination
            @current-change="changeHandler"
            :page-sizes="[2, 5, 10, 15,20]"
            :page-size="3"
            layout="total, sizes, prev, pager, next, jumper"
            :total="16">
            </el-pagination>
        </div>
        <!-- 分页结束 -->
        <!-- 模态框开始 -->
        <el-dialog :title="title" :visible.sync="dialogFormVisible">
            <!-- 表单开始 -->
        <el-form :model="ruleForm"  ref="ruleForm" label-width="100px" class="demo-ruleForm">
        <el-form-item label="产品名称" prop="name">
            <el-input v-model="ruleForm.name"></el-input>
        </el-form-item>
        <el-form-item label="所属栏目" prop="region">
            <el-select v-model="ruleForm.region" placeholder="请选择活动区域">
            <el-option label="123" value='0'></el-option>
            <el-option label="家庭保洁" value='1'></el-option>
            <el-option label="衣物护理" value='2'></el-option>
            <el-option label="母婴陪护" value='3'></el-option>
            <el-option label="家具养护" value='4'></el-option>
            <el-option label="家电清洗" value='5'></el-option>
            <el-option label="环境整治" value='6'></el-option>
            </el-select>
        </el-form-item>
        <el-form-item label="价格" prop="price">
            <el-input v-model="ruleForm.price"></el-input>
        </el-form-item>
        <el-form-item label="封面" prop="name">
            <el-upload
                class="avatar-uploader"
                action="http://81.69.24.232/pet/jz_upload.php"
                :show-file-list="false"
                :on-success="successHandler">
                <img v-if="icon" :src="icon" class="avatar">
                <i v-else class="el-icon-plus avatar-uploader-icon"></i>
            </el-upload>
          </el-form-item>
        <el-form-item label="产品描述" prop="desc">
            <el-input type="textarea" v-model="ruleForm.desc"></el-input>
        </el-form-item>
        </el-form>
        <!-- 表单结束 -->
            <div slot="footer" class="dialog-footer">
            <el-button size='mini' @click="dialogFormVisible = false">取 消</el-button>
            <el-button size='mini' type="primary" @click="submitHandler">确 定</el-button>
            </div>
        </el-dialog>
        <!-- 模态框结束 -->
    </div>
</template>
<script>
import {mapState,mapActions} from 'vuex'
export default {
    data(){
        return{
           list:{
               page:1,
               pageSize:10
           },
           title:'添加产品信息',
           dialogFormVisible:false,
           select:'',
           select1:'',
           ruleForm:{},
           icon:'',
        }
    },
    computed:{
        ...mapState('product',['products','total'])
    },
    methods:{
         ...mapActions('product',['findAllProduct','saveProduct','deleteProduct']),
        showHandler(){
            this.dialogFormVisible = true
        },
        //切换页面
        changeHandler(page){
            this.list.page = page
            this.findAllProduct(this.list)
        },
        //提交保存
        submitHandler(a){
            console.log(a);
        this.saveProduct(this.ruleForm).then(res =>{
            console.log(res,'///////////////////');
            if(res.status === 200){
            this.dialogFormVisible = false
            this.findAllProduct(this.list)
            this.$message({
                type: 'success',
                message:res.message
            })
            }else {
            this.$message({
                type: 'error',
                message:res.message
            })
            }
        })
        },
          //删除栏目信息
      deleteHandler(id){
        let params = {
          id
        }
        this.deleteProduct(params).then(res =>{
          if(res.status === 200){
            this.findAllProduct(this.list)
            this.$message({
              type:'success',
              message: res.message
            })
          }else {
              this.$message({
              type:'error',
              message: res.message
            })
          }
        })
      },
      //编辑栏目信息
    editHandler(data){
      // 将当前行的内容，赋值给我们的ruleForm
        this.ruleForm = data
        this.title = '编辑栏目信息'
      //打开模态框
        this.dialogFormVisible = true
    },
        successHandler(file){
        //   console.log(file);
        //文件上传成功，将拿到的图片地址，保存到我们栏目信息的对象中(ruleFrom)
        this.ruleForm.icon=file
      },
    },
    created(){
       this.findAllProduct(this.list)
    }
}
</script>
<style scoped>
.product .button{
    display: flex;
    
}
.product .button .bu{
    box-sizing: border-box;
    width: 55px;
    height: 27px;
    background-color: #2cb5ac;
}
.product .button .sm{
    width: 200px;
    margin-left:15px;
}
.product .button .sm1{
    margin-top: 10px;
}
.product .block{
    float: right;
}
  .avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }
  .avatar-uploader .el-upload:hover {
    border-color: #409EFF;
  }
  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    line-height: 178px;
    text-align: center;
  }
  .avatar {
    width: 178px;
    height: 178px;
    display: block;
  }
</style>